﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Blogs
{
    public partial class MainInterface : Form
    {
        public MainInterface()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void MainInterface_Load(object sender, EventArgs e)
        {
            var cmd = "select * from Articles";
            var info = DbHelper.GetDate(cmd);
            dataGridView1.DataSource = info;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var Title = textBox1.Text;
            var sql = string.Format("select * from Articles  where  Title like '%{0}%'", Title);
            var cmd = DbHelper.GetDate(sql);
            dataGridView1.DataSource = cmd;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Add form3 = new Add();
            var res = form3.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var Title = textBox1.Text;

                var cmd = string.Format("select * from Articles where Title like '%{0}%'", Title);

                var dt = DbHelper.GetDate(cmd);
                dataGridView1.DataSource = dt;
            }
    }

        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var shan = dataGridView1.SelectedRows[0];

                var id = (int)shan.Cells["Id"].Value;

                var qus = MessageBox.Show("是否删除", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (qus.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where id = {0}", id);

                    var coon = DbHelper.Delete(sql);
                    if (coon > 0)
                    {
                        var Title = textBox1.Text;

                        var cmd = string.Format("select * from Articles where Title like '%{0}%'", Title);

                        var dt = DbHelper.GetDate(cmd);

                        dataGridView1.DataSource = dt;

                        MessageBox.Show("已删除！");
                    }
                }
            }
        }
    }
